﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Payment.Contract.DataContract.History;
using ElectronicCommerce.Framework.Payment.Data.Interfaces;

namespace ElectronicCommerce.Framework.Payment.Data
{
    public static class Checker
    {

        /// <summary>
        /// 是否已经支付过
        /// </summary>
        /// <param name="soID"></param>
        /// <returns></returns>
        public static bool IsPaid(string soID)
        {
            bool isDuplicatePay = false;

            IChargeHistoryRepository chargeHistoryRepository = RepositoryFactory.CreateChargeHistoryRepository();
            ChargeHistory chargeHistory = chargeHistoryRepository.GetChargeHistory(soID);
            if (chargeHistory != null && chargeHistory.ChargeStatus == PaymentStatus.Success)
            {
                isDuplicatePay = true;
            }

            return isDuplicatePay;
        }

        /// <summary>
        /// 检查金额是否足以退款
        /// </summary>
        /// <param name="soID"></param>
        /// <param name="refundAmount"></param>
        /// <returns></returns>
        public static bool SOAmountIsInsufficientForRefund(string soID, decimal refundAmount)
        {
            bool isInsufficient = true;

            // 获取订单金额
            IChargeHistoryRepository chargeHistoryRepository = RepositoryFactory.CreateChargeHistoryRepository();

            ChargeHistory chargeHistory = chargeHistoryRepository.GetChargeHistory(soID);
            if (chargeHistory == null)
                throw new PaymentException(PaymentExceptionType.NoPaid);
            decimal soAmount = chargeHistory.SOAmount;

            // 获取所有已经退款的金额
            IRefundHistoryRepository refundHistoryRepository = RepositoryFactory.CreateRefundHistoryRepository();
            List<RefundHistory> refundHistoryList = refundHistoryRepository.GetRefundHistoryList(soID);

            decimal refundedAmount = 0;
            if (refundHistoryList != null && refundHistoryList.Count > 0)
            {
                foreach (RefundHistory refundHistory in refundHistoryList)
                {
                    refundedAmount = refundedAmount + refundHistory.FactRefundAmount;
                }
            }

            if (soAmount < refundedAmount + refundAmount)
                isInsufficient = false;

            return isInsufficient;
        }

        /// <summary>
        /// 检查是否频繁退款
        /// </summary>
        /// <param name="soID"></param>
        /// <returns></returns>
        public static bool IsFrequentRefund(string soID)
        {
            bool isFrequentRefund = true;

            IRefundHistoryRepository refundHistoryRepository = RepositoryFactory.CreateRefundHistoryRepository();
            DateTime lastRefundDate = refundHistoryRepository.GetLastRefundDate(soID);
            if (lastRefundDate.CompareTo(DateTime.Now.AddMinutes(-10)) > 0)
                isFrequentRefund = false;

            return isFrequentRefund;
        }
    }
}
